home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / WIN_PRO / FRMSUB.ZIP;1 / TOOLBAR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-07  |  13.8 KB  |  334 lines

  1. /* ********************************************************************** */
  2. /*                                                                        */
  3. /*   ToolBar  Main Module                                                 */
  4. /*                                                                        */
  5. /*   This sample program implements two menu bars: a standard action bar  */
  6. /*   and a "toolbar". The toolbar consists of "buttons" that can be       */
  7. /*   pressed. Currently the buttons take no actions.                      */
  8. /*                                                                        */
  9. /*   The purpose of this sample is to demonstrate subclassing the frame   */
  10. /*   window to add frame controls.                                        */
  11. /*                                                                        */
  12. /*                                                                        */
  13. /*   DISCLAIMER OF WARRANTIES.  The following [enclosed] code is          */
  14. /*   sample code created by IBM Corporation. This sample code is not      */
  15. /*   part of any standard or IBM product and is provided to you solely    */
  16. /*   for  the purpose of assisting you in the development of your         */
  17. /*   applications.  The code is provided "AS IS", without                 */
  18. /*   warranty of any kind.  IBM shall not be liable for any damages       */
  19. /*   arising out of your use of the sample code, even if they have been   */
  20. /*   advised of the possibility of   such damages.                        */
  21. /*                                                                        */
  22. /*   Copyright 1992, IBM Corp                                             */
  23. /* ********************************************************************** */
  24.  
  25.  
  26. #define INCL_WIN
  27. #define INCL_GPI
  28. #define INCL_DOS
  29. #define INCL_DOSMISC
  30. #define INCL_DOSERRORS
  31. #include <os2.h>
  32.  
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include "toolbar.h"
  36.  
  37.  
  38.  
  39.  
  40. HAB     hab;
  41. HMQ     hmq;
  42. HWND    hwndClient;
  43. HWND    hwndFrame;
  44. HWND    hwndToolBar ;
  45. HWND    hwndMenuBar ;
  46. QMSG    qmsg;
  47.  
  48. PSZ    szClassName  = "ToolBarClass" ;
  49. PSZ    szMainTitle  = "ToolBar" ;
  50. PSZ    szErrorTitle = "ToolBar Error" ;
  51.  
  52.         /* ----------------  Prototypes  ------------------------ */
  53. MRESULT EXPENTRY MainWindowProc( HWND, USHORT, MPARAM, MPARAM );
  54. MRESULT EXPENTRY NewFrameProc( HWND, USHORT, MPARAM, MPARAM );
  55. VOID             ShowErrorWindow( PSZ, BOOL );
  56.  
  57.  
  58.  
  59. /* ********************************************************************** */
  60. /*                                                                        */
  61. /*   Main                                                                 */
  62. /*                                                                        */
  63. /* ********************************************************************** */
  64.  
  65. VOID main()
  66. {
  67.  
  68.   if ( (hab = WinInitialize( 0L )) == (HAB) NULL ){
  69.      printf( "ToolBar Error:  WinInitialize failed \n" );
  70.      return;
  71.   }
  72.   else {
  73.      if ( (hmq = WinCreateMsgQueue( hab, 0 )) == (HMQ) NULL ){
  74.         printf( "ToolBar Error:  WinCreateMsgQueue failed \n" );
  75.         return;
  76.      }
  77.      else {
  78.  
  79.        ULONG fulCreate= FCF_TITLEBAR | FCF_SYSMENU | FCF_SIZEBORDER |
  80.                         FCF_MINMAX | FCF_SHELLPOSITION | FCF_ICON  ;
  81.                /*
  82.                 *  Note: no menu was specificed in create flags
  83.                 */
  84.  
  85.         WinSetPointer( HWND_DESKTOP,
  86.                        WinQuerySysPointer(HWND_DESKTOP,SPTR_WAIT,TRUE));
  87.  
  88.         WinRegisterClass(hab, szClassName, (PFNWP)MainWindowProc, CS_SIZEREDRAW, 0);
  89.  
  90.         hwndFrame = WinCreateStdWindow(HWND_DESKTOP,
  91.                                        0L,
  92.                                        (PULONG)&fulCreate,
  93.                                        szClassName ,
  94.                                        szMainTitle,
  95.                                        0L,
  96.                                        (HMODULE)NULL,
  97.                                        ID_MAIN_WIN,
  98.                                        &hwndClient);
  99.         if ( hwndFrame == NULLHANDLE ) {
  100.            ShowErrorWindow( "Error creating Main window !", TRUE );
  101.         }
  102.         else {
  103.            PFNWP     pfnwpOldFrameProc ;
  104.  
  105.              /* ---------  subclass frame proc  ------------------ */
  106.            pfnwpOldFrameProc = WinSubclassWindow( hwndFrame,
  107.                                                   (PFNWP) NewFrameProc );
  108.            if ( pfnwpOldFrameProc == (PFNWP)0L ){
  109.                ShowErrorWindow( "Error subclassing frame window !", TRUE );
  110.            }
  111.            else {
  112.               PID       pid ;
  113.               SWCNTRL   swCntrl;
  114.               HSWITCH   hSwitch ;
  115.               LONG      lRGB;
  116.  
  117.                 /* -------  store old frame proc with handle  ------- */
  118.               WinSetWindowULong( hwndFrame,
  119.                                  QWL_USER,
  120.                                  (ULONG) pfnwpOldFrameProc );
  121.  
  122.                 /* ------------------ load menus  ------------------- */
  123.               hwndMenuBar = WinLoadMenu( hwndFrame,
  124.                                          (HMODULE)NULL,
  125.                                          MID_MENUBAR );
  126.  
  127.               hwndToolBar = WinLoadMenu( hwndFrame,
  128.                                          (HMODULE)NULL,
  129.                                          MID_TOOLBAR );
  130.                 /*
  131.                  *  Note that the last menu loaded, the toolbar, is the
  132.                  *  one that is associated with the frame as "the" menu.
  133.                  *  this means that hwndMenuBar is the only link to the
  134.                  *  regular action bar, so hang onto it tightly
  135.                  */
  136.  
  137.                 /* ---------- set toolbar background color ---------- */
  138.               lRGB =  WinQuerySysColor( HWND_DESKTOP, SYSCLR_BUTTONDARK, 0L );
  139.               WinSetPresParam( hwndToolBar,
  140.                                PP_BACKGROUNDCOLOR,
  141.                                4L,
  142.                                (PVOID)lRGB );
  143.  
  144.                 /* ---------  set window size and pos  -------------- */
  145.               WinSetWindowPos( hwndFrame,
  146.                                HWND_TOP,
  147.                                0, 0, 370, 300, 
  148.                                SWP_SIZE | SWP_SHOW | SWP_ACTIVATE );
  149.  
  150.                /* ----------- add program to tasklist  --------------- */
  151.               WinQueryWindowProcess( hwndFrame, &pid, NULL );
  152.               swCntrl.hwnd = hwndFrame ;
  153.               swCntrl.hwndIcon = (HWND) NULL ;
  154.               swCntrl.hprog = (HPROGRAM) NULL ;
  155.               swCntrl.idProcess = pid ;
  156.               swCntrl.idSession = (LONG) NULL ;
  157.               swCntrl.uchVisibility = SWL_VISIBLE ;
  158.               swCntrl.fbJump = SWL_JUMPABLE ;
  159.               sprintf( swCntrl.szSwtitle, szMainTitle );
  160.               hSwitch = WinAddSwitchEntry((PSWCNTRL)&swCntrl);
  161.  
  162.  
  163.               WinSetPointer(HWND_DESKTOP,
  164.                             WinQuerySysPointer(HWND_DESKTOP,SPTR_ARROW,TRUE));
  165.  
  166.                  /* ---------- start the main processing loop ----------- */
  167.               while (WinGetMsg(hab, &qmsg,NULLHANDLE,0,0)){
  168.                   WinDispatchMsg(hab, &qmsg);
  169.               }
  170.  
  171.               WinRemoveSwitchEntry( hSwitch );
  172.            } /* end of else ( pfnwpOldFrameProc ) */
  173.  
  174.            WinSetPointer(HWND_DESKTOP,
  175.                          WinQuerySysPointer(HWND_DESKTOP,SPTR_ARROW,TRUE));
  176.            WinDestroyWindow(hwndFrame);
  177.         }  /* end of else (hwndFrame == NULLHANDLE) */
  178.  
  179.         WinSetPointer(HWND_DESKTOP,
  180.                       WinQuerySysPointer(HWND_DESKTOP,SPTR_ARROW,TRUE));
  181.         WinDestroyMsgQueue(hmq);
  182.      }  /* end of else ( ...WinCreateMsgQueue() */
  183.  
  184.    WinTerminate(hab);
  185.    }  /* end of else (...WinInitialize(NULL) */
  186. }  /*  end of main() */
  187.  
  188. /* ********************************************************************** */
  189. /*                                                                        */
  190. /*   MainWindowProc                                                       */
  191. /*                                                                        */
  192. /* ********************************************************************** */
  193.  
  194. MRESULT EXPENTRY
  195. MainWindowProc( HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2 )
  196. {
  197.  
  198.   switch (msg) {
  199.  
  200.     case WM_PAINT:
  201.        {
  202.          RECTL rectl ;
  203.          HPS   hps;
  204.  
  205.          hps = WinBeginPaint( hwnd, (HPS) NULL, &rectl );
  206.          WinFillRect( hps, (PRECTL)&rectl, SYSCLR_WINDOW);
  207.          WinEndPaint( hps );
  208.       }
  209.       break;
  210.  
  211.     default:
  212.       return WinDefWindowProc(hwnd,msg,mp1,mp2);
  213.  
  214.   } /*  end of switch () */
  215.   return( FALSE );
  216.  
  217. } /*  end of MainWindowProc */
  218. /* ********************************************************************** */
  219. /*                                                                        */
  220. /*   NewFrameProc                                                         */
  221. /*                                                                        */
  222. /*       This frame proc subclasses the original frame proc. The two      */
  223. /*   messages of interest are WM_QUERYFRAMECTLCOUNT and WM_FORMATFRAME.   */
  224. /*   By catching WM_QUERYFRAMECTLCOUNT, we can return the count of the    */
  225. /*   total number of frame controls, both original and new. This count    */
  226. /*   is used to allocate SWP structures for the array during frame        */
  227. /*   formating. When we process WM_FORMATFRAME, we first call the         */
  228. /*   the original frame proc to setup the SWP array for the original      */
  229. /*   frame controls. Note that among these "original" frame controls is   */
  230. /*   the toolbar. We will add the regular action bar as a second menu     */
  231. /*   below the toolbar. The original processing of WM_FORMATFRAME will    */
  232. /*   return the count of original frame controls (note that because we    */
  233. /*   modified WM_QUERYFRAMECTLCOUNT the SWP array will actually contain   */
  234. /*   an extra SWP for our second menu). There should probably be more     */
  235. /*   code that checks the order of controls in the array, but we are      */
  236. /*   going to make some (educated) assumptions about the control order.   */
  237. /*   The client window is always the last item in the SWP array. The      */
  238. /*   menu bar (toolbar in our case) is the previous item. We will use     */
  239. /*   the default settings of these controls to position our new menu and  */
  240. /*   make adjustments to these controls for the new menu.                 */
  241. /*                                                                        */
  242. /* ********************************************************************** */
  243.  
  244. MRESULT EXPENTRY
  245. NewFrameProc( HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2 )
  246. {
  247.   PFNWP   oldFrameProc ;
  248.  
  249.      /* ------- get original frame procedure  --------- */
  250.   oldFrameProc = (PFNWP) WinQueryWindowULong( hwnd, QWL_USER );
  251.  
  252.   switch (msg) {
  253.     case WM_QUERYFRAMECTLCOUNT :
  254.          {
  255.            USHORT   itemCount ;
  256.            
  257.                 /* ---- get count of original frame controls --- */
  258.            itemCount = SHORT1FROMMR( oldFrameProc( hwnd, msg, mp1, mp2 ));
  259.                 /* ------- add 1 for new toolbar control  ------ */
  260.            return ( (MRESULT) ++itemCount );  
  261.          }
  262.  
  263.     case WM_FORMATFRAME :
  264.        {
  265.          PSWP     pSWP ;
  266.          PRECTL   pFrameRect ;
  267.          USHORT   itemCount ;
  268.  
  269.          pFrameRect = PVOIDFROMMP( mp2 );
  270.          pSWP = PVOIDFROMMP( mp1 );
  271.            /* ---- run regular processing on original controls --- */
  272.          itemCount = SHORT1FROMMR( oldFrameProc( hwnd, msg, mp1, mp2 ));
  273.  
  274.          /* ---- get size values for 2nd menu bar  -------- */
  275.          pSWP[ itemCount ].fl = SWP_SIZE;
  276.          pSWP[ itemCount ].cy =  pSWP[(itemCount-1)].cy ;  /* set some */
  277.          pSWP[ itemCount ].cx =  pSWP[(itemCount-1)].cx ;  /*  defaults */
  278.          pSWP[ itemCount ].hwndInsertBehind = HWND_TOP ;
  279.          pSWP[ itemCount ].hwnd = hwndMenuBar ;
  280.          WinSendMsg( hwndMenuBar,                 /* let the menu code make */
  281.                      WM_ADJUSTWINDOWPOS,          /*  the actual size       */
  282.                      MPFROMP( pSWP+itemCount ),   /*  adjustments           */
  283.                      (MPARAM) 0L );
  284.  
  285.           /* ------ position menu directly below other menu  ------- */
  286.          pSWP[itemCount].x = pSWP[(itemCount-2)].x ; 
  287.          pSWP[itemCount].y = pSWP[(itemCount-2)].y - pSWP[itemCount].cy ;
  288.          pSWP[itemCount].fl = pSWP[(itemCount-2)].fl ;
  289.           /* --------  adjust client window size for 2nd menu ------- */
  290.          pSWP[(itemCount-1)].cy -= pSWP[itemCount].cy ;
  291.           /* --------  return total count of frame controls   ------- */
  292.          return( MRFROMSHORT( ++itemCount )  );
  293.        }
  294.        break;
  295.  
  296.     default:
  297.       return oldFrameProc(hwnd,msg,mp1,mp2);
  298.  
  299.   } /* end of switch () */
  300.  
  301.   return( FALSE );
  302.  
  303. } /* end of NewFrameProc */
  304. /* ********************************************************************** */
  305. /*                                                                        */
  306. /*   ShowErrorWindow                                                      */
  307. /*                                                                        */
  308. /* ********************************************************************** */
  309. VOID
  310. ShowErrorWindow( PSZ  pszErrorMsg, BOOL bUseLastError )
  311. {
  312.   CHAR      acErrorBuffer[256] ;
  313.  
  314.   if ( bUseLastError ) {
  315.       ERRORID   errorID = WinGetLastError( hab );
  316.  
  317.       sprintf( acErrorBuffer,
  318.                "%s \n(code = 0x%lX)",
  319.                pszErrorMsg,
  320.                (ULONG) errorID );
  321.       pszErrorMsg = (PSZ) acErrorBuffer ;
  322.   }  /* end of if ( bUseLastError ) */
  323.  
  324.   WinMessageBox( HWND_DESKTOP,
  325.                  HWND_DESKTOP,
  326.                  pszErrorMsg ,
  327.                  szErrorTitle ,
  328.                  0,
  329.                  MB_CUACRITICAL | MB_OK );
  330.  
  331. }
  332. 
  333.  
  334.